package com.edushare.common.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Handy helper method
 * @author Naishe
 *
 */
public class StringUtil {
	
	public static void main(String[] args){
		//String str = "N!$hant_N33r^j-class:9";
//		System.out.println(str +" >>> " + replace(str, "[^A-Za-z0-9_\\-]","*"));
		String str = "..p..-.p.";
		System.out.println("input: "+str+", output: "+IsMinNoOfAlphanumerics(str, 2));
	}
	
	/**
	 * is the given input not-null, non-empty string?
	 * @param str
	 * @return
	 */
	public static boolean isValid(String str){
		boolean isValid = false;
		if(str!=null && !str.replace(" ", "").equals(""))
			isValid = true;
		return isValid;
	}
	
	/**
	 * Breaks string into logical pieces at comma, semicolon
	 * @param skillStr
	 * @return
	 */
	public static ArrayList<String> divideStringInLogicalParts(String input) {
		if(!isValid(input))
			return null;
		input = input.replace("  ", " ");//replace large spaces into single
		input = input.replace(",", ";");//changes all commas to semicolons
		input = input.replace(";;", ";");//remove any repetition of semicolon
		String[] inputArr = input.split(";");
		ArrayList<String> al = new ArrayList<String>();
		for(String item: inputArr)
			al.add(item);
		return al;
	}

	public static String replace(String input, String pattern, String replacement) {
		if(!isValid(input))
			return null;
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(input);
		StringBuffer sb = new StringBuffer();
		while(m.find()){
			m.appendReplacement(sb, replacement);
		}
		m.appendTail(sb);
		return sb.toString();
	}

	public static String replaceLongSpacesToSingle(String input) {
		if(!isValid(input))
			return null;
		input = input.replace("  ", " ");
		return input;
	}
	
	public static String formatDate(Date date){
		if(date==null){
			return "N/A";
		}
		SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy hh:mm a");
    	return formatter.format(date);
	}

	public static List<String> csvToList(String input) {
		if(!isValid(input))
			return null;
		List<String> lst = Arrays.asList(input.split(","));
		return lst;
	}

	public static String ellipsis(String input, int chopAtCharNo) {
		if(isValid(input) && input.length()>chopAtCharNo){
			input = input.substring(0, chopAtCharNo-1)+"...";
		}
		
		return input;
	}

	public static boolean IsMinNoOfAlphanumerics(String input, int n) {
		boolean isMinSatisfied = false;
		Pattern p = Pattern.compile("[0-9a-zA-Z]");
        Matcher m = p.matcher(input); // get a matcher object
        int count = 0;
        while(m.find()) {
           count++;
           if(count>=n){
        	   isMinSatisfied = true;
        	   break;
           }
        }
		return isMinSatisfied;
	}
	
}
